package com.ruoyi.useradmin.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.useradmin.mapper.UaCollectMapper;
import com.ruoyi.useradmin.service.IUaCollectService;
import com.ruoyi.useradmin.domain.UaCollect;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Value;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.useradmin.domain.UaUser;
import com.ruoyi.useradmin.service.IUaUserService;
import com.ruoyi.activieadmin.service.IAaActiveService;
import com.ruoyi.campadmin.service.ICaAmenityService;
import com.ruoyi.campadmin.service.ICaItemRentalService;
import com.ruoyi.campadmin.service.ICaItemSaleService;
import com.ruoyi.campadmin.service.ICaVenueRentalService;
import com.ruoyi.traveladmin.service.ITaTravelService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Map;

/**
 * @Description  
 * @Author kemonvvv
 * @Date 2023-06-07 
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class UaCollectServiceImpl implements IUaCollectService {


    @Resource
    private UaCollectMapper uaCollectMapper;
    
    
    @Resource
    private IUaUserService uaUserService;

    //评论类型[1场地租赁/2单品租赁/3单品售卖/4设施/5活动/6游记]
    //1
    @Resource
    private ICaVenueRentalService caVenueRentalService;
    //2
    @Resource
    private ICaItemRentalService caItemRentalService;
    //3
    @Resource
    private ICaItemSaleService caItemSaleService;
    //4
    @Resource
    private ICaAmenityService caAmenityService;
    //5
    @Resource
    private IAaActiveService aaActiveService;
    //6
    @Resource
    private ITaTravelService taTravelService;


    
    
    /**
     * 全查询
     * 
     * @param pageDomain 分页信息
     * @param uaCollect 要查询的实体类
     * @return 结果
     */
    @Override
    public Page<UaCollect> getUaCollectPage(PageDomain pageDomain, UaCollect uaCollect) {
        if (Objects.isNull(uaCollect)) {
            return new Page<>();
        }
        try {
            LambdaQueryWrapper<UaCollect> queryWrapper = new LambdaQueryWrapper<>(new UaCollect());
            //if(!Objects.isNull(uaCollect.getName())){
            //	queryWrapper.like(UaCollect::getName,uaCollect.getName());
            //}
            

            if (!Objects.isNull(uaCollect.getId())) {
                queryWrapper.eq(UaCollect::getId, uaCollect.getId());
            }

            if (!Objects.isNull(uaCollect.getUaUserId())) {
                queryWrapper.eq(UaCollect::getUaUserId, uaCollect.getUaUserId());
            }

            if (!Objects.isNull(uaCollect.getCollectType())) {
                queryWrapper.like(UaCollect::getCollectType, uaCollect.getCollectType());
            }

            if (!Objects.isNull(uaCollect.getCollectTypeId())) {
                queryWrapper.eq(UaCollect::getCollectTypeId, uaCollect.getCollectTypeId());
            }

            if (!Objects.isNull(uaCollect.getStatus())) {
                queryWrapper.like(UaCollect::getStatus, uaCollect.getStatus());
            }

            if (!Objects.isNull(uaCollect.getDelFlag())) {
                queryWrapper.like(UaCollect::getDelFlag, uaCollect.getDelFlag());
            }

            if (!Objects.isNull(uaCollect.getCreateBy())) {
                queryWrapper.like(UaCollect::getCreateBy, uaCollect.getCreateBy());
            }

            if (!Objects.isNull(uaCollect.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(uaCollect.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(uaCollect.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(UaCollect::getCreateTime, uaCollect.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(uaCollect.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(UaCollect::getCreateTime, uaCollect.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(uaCollect.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(uaCollect.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(UaCollect::getCreateTime, uaCollect.getParams().get("CreateTimeBeginTime"), uaCollect.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(uaCollect.getUpdateBy())) {
                queryWrapper.like(UaCollect::getUpdateBy, uaCollect.getUpdateBy());
            }

            if (!Objects.isNull(uaCollect.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(uaCollect.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(uaCollect.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(UaCollect::getUpdateTime, uaCollect.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(uaCollect.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(UaCollect::getUpdateTime, uaCollect.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(uaCollect.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(uaCollect.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(UaCollect::getUpdateTime, uaCollect.getParams().get("UpdateTimeBeginTime"), uaCollect.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(uaCollect.getRemark())) {
                queryWrapper.like(UaCollect::getRemark, uaCollect.getRemark());
            }
        Page<UaCollect> uaCollectList= uaCollectMapper.selectPage(pageDomain.getPage(), queryWrapper);
        for(UaCollect record:uaCollectList.getRecords()){
            record.setUaUser(uaUserService.getUaUserById(record.getUaUserId()));
            //收藏类型[1场地租赁/2单品租赁/3单品售卖/4设施/5活动/6游记]
            switch (record.getCollectType()){
                case "1":
                    record.setCaVenueRental(caVenueRentalService.getCaVenueRentalById(record.getCollectTypeId()));
                    break;
                case "2":
                    record.setCaItemRental(caItemRentalService.getCaItemRentalById(record.getCollectTypeId()));
                    break;
                case "3":
                    record.setCaItemSale(caItemSaleService.getCaItemSaleById(record.getCollectTypeId()));
                    break;
                case "4":
                    record.setCaAmenity(caAmenityService.getCaAmenityById(record.getCollectTypeId()));
                    break;
                case "5":
                    record.setAaActive(aaActiveService.getAaActiveById(record.getCollectTypeId()));
                    break;
                case "6":
                    record.setTaTravel(taTravelService.getTaTravelById(record.getCollectTypeId()));
                    break;
            }
        }

            return uaCollectList;
        } catch (Exception e) {
            e.printStackTrace();
            return new Page<>();
        }
    }
    

    
    /**
     * 查询通过id
     * 
     * @param id 实体类的主键
     * @return 结果
     */
    @Override
    public UaCollect getUaCollectById(Long id) {
        if (Objects.isNull(id)) {
            return new UaCollect();
        }
        try {
            UaCollect uaCollect = uaCollectMapper.selectById(id);
            uaCollect.setUaUser(uaUserService.getUaUserById(uaCollect.getUaUserId()));
                        //收藏类型[1场地租赁/2单品租赁/3单品售卖/4设施/5活动/6游记]
                        switch (    uaCollect.getCollectType()){
                case "1":
                        uaCollect.setCaVenueRental(caVenueRentalService.getCaVenueRentalById(uaCollect.getCollectTypeId()));
                    break;
                case "2":
                        uaCollect.setCaItemRental(caItemRentalService.getCaItemRentalById(uaCollect.getCollectTypeId()));
                    break;
                case "3":
                        uaCollect.setCaItemSale(caItemSaleService.getCaItemSaleById(uaCollect.getCollectTypeId()));
                    break;
                case "4":
                        uaCollect.setCaAmenity(caAmenityService.getCaAmenityById(uaCollect.getCollectTypeId()));
                    break;
                case "5":
                        uaCollect.setAaActive(aaActiveService.getAaActiveById(uaCollect.getCollectTypeId()));
                    break;
                case "6":
                        uaCollect.setTaTravel(taTravelService.getTaTravelById(uaCollect.getCollectTypeId()));
                    break;
            }
                        if (Objects.isNull(uaCollect)){
                uaCollect = new UaCollect();
            }
            return uaCollect;

        } catch (Exception e) {
            e.printStackTrace();
            return new UaCollect();
        }
    }


    
    
    /**
     * 通过类查询的属性值 id列表
     * 
     * @param uaCollect 要查询的实体类
     * @return 结果
     */
    @Override
    public List<Long> getIdListByUaCollect(UaCollect uaCollect) {
        if (Objects.isNull(uaCollect)) {
            return new ArrayList<>();
        }
        try {
            LambdaQueryWrapper<UaCollect> queryWrapper = new LambdaQueryWrapper<>(new UaCollect());
            

            if (!Objects.isNull(uaCollect.getId())) {
                queryWrapper.eq(UaCollect::getId, uaCollect.getId());
            }

            if (!Objects.isNull(uaCollect.getUaUserId())) {
                queryWrapper.eq(UaCollect::getUaUserId, uaCollect.getUaUserId());
            }

            if (!Objects.isNull(uaCollect.getCollectType())) {
                queryWrapper.like(UaCollect::getCollectType, uaCollect.getCollectType());
            }

            if (!Objects.isNull(uaCollect.getCollectTypeId())) {
                queryWrapper.eq(UaCollect::getCollectTypeId, uaCollect.getCollectTypeId());
            }

            if (!Objects.isNull(uaCollect.getStatus())) {
                queryWrapper.like(UaCollect::getStatus, uaCollect.getStatus());
            }

            if (!Objects.isNull(uaCollect.getDelFlag())) {
                queryWrapper.like(UaCollect::getDelFlag, uaCollect.getDelFlag());
            }

            if (!Objects.isNull(uaCollect.getCreateBy())) {
                queryWrapper.like(UaCollect::getCreateBy, uaCollect.getCreateBy());
            }

            if (!Objects.isNull(uaCollect.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(uaCollect.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(uaCollect.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(UaCollect::getCreateTime, uaCollect.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(uaCollect.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(UaCollect::getCreateTime, uaCollect.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(uaCollect.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(uaCollect.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(UaCollect::getCreateTime, uaCollect.getParams().get("CreateTimeBeginTime"), uaCollect.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(uaCollect.getUpdateBy())) {
                queryWrapper.like(UaCollect::getUpdateBy, uaCollect.getUpdateBy());
            }

            if (!Objects.isNull(uaCollect.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(uaCollect.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(uaCollect.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(UaCollect::getUpdateTime, uaCollect.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(uaCollect.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(UaCollect::getUpdateTime, uaCollect.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(uaCollect.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(uaCollect.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(UaCollect::getUpdateTime, uaCollect.getParams().get("UpdateTimeBeginTime"), uaCollect.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(uaCollect.getRemark())) {
                queryWrapper.like(UaCollect::getRemark, uaCollect.getRemark());
            }

            List<UaCollect> uaCollects = uaCollectMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (UaCollect uaCollectNo:uaCollects){
                idList.add(uaCollectNo.getId());
            }
            return idList;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }


    /**
     * 通过uaUser外键idList查询idList
     *
     * @param uaUserIdList 要查询的实体类
     * @return 结果
     **/
    public List<Long> getIdListByUaUserIdList(List<Long> uaUserIdList) {
        if (Objects.isNull(uaUserIdList)){
            return  new ArrayList<>();
        }
        try{
            LambdaQueryWrapper<UaCollect> queryWrapper = new LambdaQueryWrapper<>(new UaCollect());
            queryWrapper.in(UaCollect::getUaUserId, uaUserIdList);
            List<UaCollect> uaCollects = uaCollectMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (UaCollect uaCollect:uaCollects){
                idList.add(uaCollect.getId());
            }
            return idList;
        }catch (Exception e){
            return new ArrayList<>();
        }
    }


    /**
     * 通过collectType外键idList查询idList
     *
     * @param collectTypeIdList 要查询的实体类
     * @return 结果
     **/
    public List<Long> getIdListByCollectTypeIdList(List<Long> collectTypeIdList) {
        if (Objects.isNull(collectTypeIdList)){
            return  new ArrayList<>();
        }
        try{
            LambdaQueryWrapper<UaCollect> queryWrapper = new LambdaQueryWrapper<>(new UaCollect());
            queryWrapper.in(UaCollect::getCollectTypeId, collectTypeIdList);
            List<UaCollect> uaCollects = uaCollectMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (UaCollect uaCollect:uaCollects){
                idList.add(uaCollect.getId());
            }
            return idList;
        }catch (Exception e){
            return new ArrayList<>();
        }
    }

    
    /**
     * 添加
     * 
     * @param uaCollect 要添加的实体类
     * @return 结果
     */
    @Override
    public int insertUaCollect(UaCollect uaCollect) {
        if (Objects.isNull(uaCollect)) {
            return -1;
        }
        try {
        
            //uaCollect.setSpell(PinYin.getPinYin(uaCollect.getName()));
            return uaCollectMapper.insert(uaCollect);
        } catch (Exception e) {
            return -1;
        }
    }

    /**
     * 更新通过id进行更新一条
     * 
     * @param uaCollect 实体类
     * @return 结果
     */
    @Override
    public int updateUaCollectById(UaCollect uaCollect) {
        if (Objects.isNull(uaCollect)) {
            return -1;
        }
        try {
            //uaCollect.setSpell(PinYin.getPinYin(uaCollect.getName()));
            return uaCollectMapper.updateById(uaCollect);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 删除通过id集合
     * 
     * @param idList id集合
     * @return 结果
     */
    @Override
    public int deleteUaCollectByIdList(List idList){
        if (Objects.isNull(idList)) {
            return -1;
        }
        try {
            return uaCollectMapper.deleteBatchIds(idList);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 查询无条件全部 用于导出
     * 
     * @return 结果
     */
    @Override
    public List<UaCollect> getUaCollectList() {
        try {
            LambdaQueryWrapper<UaCollect> queryWrapper = new LambdaQueryWrapper<>(new UaCollect());
            queryWrapper.isNotNull(UaCollect::getId);
            return uaCollectMapper.selectList(queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

}
